﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

using Configuration = Refix.Core.Configuration;

namespace Refix.Repository
{
    public class MvcApplication : HttpApplication
    {
        static private void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Get.aspx",
                "Get.aspx/{component}/{version}",
                new { controller = "Component", action = "Get", component = "", version = "" });

            routes.MapRoute(
                "Get",
                "Get/{component}/{version}",
                new { controller = "Component", action = "Get", component = "", version = "" });

            routes.MapRoute(
                "GetRevision.aspx",
                "GetRevision.aspx/{component}",
                new { controller = "Component", action = "GetRevision", component = "" });

            routes.MapRoute(
                "GetRevision",
                "GetRevision/{component}",
                new { controller = "Component", action = "GetRevision", component = "" });

            routes.MapRoute(
                "Upload.aspx",
                "Upload.aspx/{component}/{version}",
                new { controller = "Component", action = "Upload", component = "", version = "" });

            routes.MapRoute(
                "Upload",
                "Upload/{component}/{version}",
                new { controller = "Component", action = "Upload", component = "", version = "" });

            routes.MapRoute(
                "AcceptUpload.aspx",
                "AcceptUpload.aspx/{component}/{version}/{email}",
                new { controller = "Component", action = "AcceptUpload", component = "", version = "" });

            routes.MapRoute(
                "AcceptUpload",
                "AcceptUpload/{component}/{version}/{email}",
                new { controller = "Component", action = "AcceptUpload", component = "", version = "" });

            routes.MapRoute(
                "Synchronize.aspx",
                "Synchronize.aspx/{component}/{email}",
                new { controller = "Component", action = "Synchronize", component = "" });

            routes.MapRoute(
                "Synchronize",
                "Synchronize/{component}}/{email}",
                new { controller = "Component", action = "Synchronize", component = "" });

            routes.MapRoute(
                "Browse.aspx",
                "Browse.aspx/{path}",
                new { controller = "Home", action = "Index", path = "" });

            routes.MapRoute(
                "Controller only",
                "{controller}",
                new { controller = "Home", action = "Index", path = "" });

            routes.MapRoute(
                "Moderation.aspx",
                "Moderation.aspx/{action}/{identifier}",
                new { controller = "Moderation" });

            routes.MapRoute(
                "Moderation",
                "Moderation/{action}/{identifier}",
                new { controller = "Moderation" });

            routes.MapRoute(
                "Default.aspx",
                "{controller}.aspx/{action}",
                new { controller = "Home", action = "Index" });

            routes.MapRoute(
                "Default",
                "{controller}/{action}",
                new { controller = "Home", action = "Index" });
        }

        protected void Application_Start()
        {
            string appSetting = Configuration.LocalRepository;
            if (string.IsNullOrEmpty(appSetting))
            {
                ConfigurationManager.AppSettings.Set("Refix.Repository.Local",
                                                     Path.Combine(Server.MapPath("~/"), "..\\.testremoterefixrepo"));
                Configuration.Refresh();
            }
            else if (appSetting.StartsWith("~"))
            {
                ConfigurationManager.AppSettings.Set("Refix.Repository.Local",
                                                     Server.MapPath(appSetting));
                Configuration.Refresh();
            }
            RegisterRoutes(RouteTable.Routes);
        }
    }
}